Ext.define('TS.controller.AccordionMenu',{
    extend: 'Ext.app.Controller',
    refs:[
        {ref: 'accmenu',selector: 'accmenu'},
        {ref: 'tabPanel',selector:'TStablepanel'}
    ],
    require:[
	    'TS.model.AccordionMenuModel'
	],
    //stores: ['BaStore'],
    init:function(){
        this.control({
            'viewport':{
            		afterrender:function(){
	            		Ext.getBody().mask('正在加载系统菜单....');
							ajax({
								url : "menu/menuroot.do",// 获取面板的地址
								params : {
									action : "list"
								},
								callback : this.addTree
					     });					     
            		}
            },
            'viewport > accmenu':{
            	afterrender:this.initLoginUser()//设置用户默认部门
            },
            'accmenu combobox':{
            	select:this.invDeptChange
            }
        });
        
    },
    invDeptChange:function(combo, records, eOpts){

     	Ext.Ajax.request({
				    url:'menu/setUserLoginDept.do',
				    method : 'POST',
				    timeout :3000,
				    jsonData:records[0].getData(),
				    success:function(response){		
				    	var rs=Ext.decode(response.responseText);
				    	if(rs.success==true){
					    	loginUser.logonDept=records[0].getData();
					    }else{
				    		showExtReturnMsg(response);
					    }
				    }
	    });
    },
    initLoginUser:function(){
    	ajax({
			url : "menu/getuser.do",
			callback :function(data){
				loginUser=data;
				
				var tree=Ext.getCmp("accmenu"); 
				var cbx=tree.down('combobox');
				cbx.store.loadData(loginUser.depts);
				Ext.Array.each(loginUser.depts,function(dept){
					if(dept.defaultDept){	
						loginUser.logonDept=dept;
						cbx.setValue(dept.deptId);
					}
				});
				Ext.getCmp("app-header").down('button').setText(loginUser.userName);
			}
     	});
    	
    },
    loadMenu:function(selModel, record){
	    if (record.get('leaf')) { 
            var panel = Ext.getCmp(record.get('id')); 
            if(!panel){
                panel=Ext.create(record.get('component'),{closable : true});
			    this.openTab(panel,record.get('id'));
			 }else{
			    var main = Ext.getCmp("syscontent");
			    main.setActiveTab(panel); 
		     }
       }
	 },

    openTab : function (panel,id){ 
        var o = (typeof panel == "string" ? panel : id || panel.id);
        var main = Ext.getCmp("syscontent");
        var tab = main.getComponent(o);      
        if (tab) {
            main.setActiveTab(tab); 
        } else if(typeof panel!="string"){ 
            panel.id = o; 
            var p = main.add(panel); 
            main.setActiveTab(p); 
        }    
    },
    addTree:function(data) {
    	Ext.getBody().unmask();
    	Ext.create('TS.model.AccordionMenuModel');
    	
    	var createStore=function(id) { // 创建树面板数据源
			return Ext.create("Ext.data.TreeStore", {
						defaultRootId : id, // 默认的根节点id
						
						model : 'TS.model.AccordionMenuModel',
						proxy : {
							type : "ajax", // 获取方式
							url : "menu/menuchild.do" // 获取树节点的地址
						},
						reader: {
					        type: 'json',
					        root:''
					    },
						clearOnLoad : true,
						nodeParam : "id"// 设置传递给后台的参数名,值是树节点的id属性
					});
		};
    	var tree=Ext.getCmp("accmenu");
			for (var i = 0; i < data.length; i++) {
				tree.add(Ext.create("Ext.tree.Panel", {
							title : data[i].text,
							iconCls : data[i].iconCls,
							//useArrows: true,
							autoScroll : true,
							rootVisible : false,
							viewConfig : {
								loadingText : "正在加载..."
							},
							store :createStore(data[i].id),
							listeners : {
								afterlayout : function() {
									if (this.getView().el) {
										var el = this.getView().el;
										var table = el
												.down("table.x-grid-table");
										if (table) {
											table.setWidth(el.getWidth());
										}
									}
								},
								itemclick : function(view,record){
									var tab=Ext.getCmp('syscontent');
									if (record.get('leaf')) { //判断是否是根节点
										if(record.get('resType') === 'URL'){ //判断资源类型
											var panel = Ext.create('Ext.panel.Panel',{
												title : record.get('text'),
												closable : true,
												iconCls : record.get('iconCls'),
												html : '<iframe width="100%" height="100%" frameborder="0" src="'+record.get('component')+'"></iframe>'
											});
											tab.add(panel);
											tab.setActiveTab(panel);
										}else if(record.get('resType') === 'COMPONENT'){
											var panel = Ext.create(record.get('component'),{
												title : record.get('text'),
												closable : true,
												iconCls : record.get('iconCls')
											});
											tab.add(panel);
											tab.setActiveTab(panel);
										}
									}
								}
							}
						}));
				tree.doLayout();
		}
    }
});
